package com.jweb.framework.ws.soap.axiom;

import java.util.Locale;

import javax.xml.namespace.QName;

import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPFault;
import org.apache.axiom.soap.SOAPFaultCode;
import org.apache.axiom.soap.SOAPFaultReason;
import org.apache.axiom.soap.SOAPFaultText;
import org.apache.axiom.soap.SOAPFaultValue;
import org.apache.axiom.soap.SOAPProcessingException;
import org.springframework.util.Assert;
import org.springframework.ws.soap.soap12.Soap12Body;
import org.springframework.ws.soap.soap12.Soap12Fault;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：
 */
class AxiomSoap12Body extends AxiomSoapBody implements Soap12Body {
	
	AxiomSoap12Body(SOAPBody axiomBody , SOAPFactory axiomFactory , boolean payloadCaching){
		super(axiomBody, axiomFactory, payloadCaching);
	}
	
	public Soap12Fault addMustUnderstandFault(String reason , Locale locale) {
		Assert.notNull(locale, "No locale given");
		SOAPFault fault = addStandardFault("MustUnderstand" , reason , locale);
		return new AxiomSoap12Fault(fault, getAxiomFactory());
	}
	
	public Soap12Fault addClientOrSenderFault(String reason , Locale locale){
		Assert.notNull(locale , "No locale given");
		SOAPFault fault = addStandardFault("Sender" , reason , locale);
		return new AxiomSoap12Fault(fault , getAxiomFactory());
	}
	
	public Soap12Fault addServerOrReceiverFault(String reason , Locale locale) {
		Assert.notNull(locale , "No locale given");
		SOAPFault fault = addStandardFault("Receiver" , reason , locale);
		return new AxiomSoap12Fault(fault , getAxiomFactory());
	}
	
	public Soap12Fault addVersionMismatchFault(String reason , Locale locale){
		Assert.notNull(locale, "No locale given");
		SOAPFault fault = addStandardFault("VersionMismatch", reason, locale);
		return new AxiomSoap12Fault(fault, getAxiomFactory());
	}
	
	public Soap12Fault addDataEncodingUnknownFault(QName[] subcodes , String reason , Locale locale){
		Assert.notNull(locale, "No locale given");
		SOAPFault fault = addStandardFault("DataEncodingUnknown" , reason , locale);
		return new AxiomSoap12Fault(fault, getAxiomFactory());
	}
	
	
	private SOAPFault addStandardFault(String localName , String faultStringOrReason , Locale locale) {
		Assert.notNull(faultStringOrReason, "No faultStringOrReason given");
		try {
			AxiomUtils.removeContents(getAxiomBody());
			SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
			SOAPFaultCode code = getAxiomFactory().createSOAPFaultCode(fault);
			SOAPFaultValue value = getAxiomFactory().createSOAPFaultValue(code);
			value.setText(fault.getNamespace().getPrefix()+":"+localName);
			SOAPFaultReason reason = getAxiomFactory().createSOAPFaultReason(fault);
			SOAPFaultText text = getAxiomFactory().createSOAPFaultText(reason);
			if(locale != null){
				text.setLang(AxiomUtils.toLanguage(locale));
			}
			text.setText(faultStringOrReason);
			return fault ;
		} catch (SOAPProcessingException e) {
			throw new AxiomSoapFaultException(e);
		}
	}
	
	public Soap12Fault getFault(){
		SOAPFault axiomFault = getAxiomBody().getFault();
		return axiomFault != null ? new AxiomSoap12Fault(axiomFault, getAxiomFactory()) : null ;
	}
	
}
